<?php
namespace Fwadmin\Controller;
use Think\Controller;
class ManageController extends BaseController{
	public function index(){
		if($_GET['p']==''){
			$_GET['p'] = 1;
		}
		$where['id'] = array('NEQ','1');
		$viewList	= M('Manage') -> where($where) -> page($_GET['p'].',15') -> select();
		$count		= M('Manage') -> where($where) -> count();
		$Pages		= new \Think\Page($count, 15); 
		$Page		->url = 'page';
		$show		= $Pages -> show();

		$this		-> assign('pages',$show);
		$this		-> assign('viewList',$viewList);
		$this		-> display();
	}
	public function add(){
		$category			= M('Category') -> where($map) -> select();
		$category			= toLevel($category,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',0);
		if(IS_AJAX){
			$Data = D("Manage");
			$admin_add = array(
				array('username','4,20','登录ID长度不正确！',0,'length'),
				array('username','','帐号名称已经存在！',1,'unique',1),
				array('password','6,30','密码长度不正确',0,'length'),
				array('repassword','password','确认密码不一致',0,'confirm'),
				array('author','require','笔名不能为空！'),
				array('phone','/^(0?1[358]\d{9})$|^((0(10|2[1-3]|[3-9]\d{2}))?[1-9]\d{6,7})$/','电话格式错误！',2,),
				array('email','email','Email格式错误！',2)
			);
			if(I("typeids") == ""){
				$date['msg']	= "负责栏目不能为空";
				$date['status']	= '0';
				$this ->ajaxReturn($date);
			}else{
				if($Data -> validate($admin_add) -> create()){
					$Data -> password = md5(I("password"));
					$Data -> typeids = implode(",", I("typeids"));
					$Data -> last_time = date("Y-m-d H:i:s");
					if($Data -> add() !== false){
						$date['msg']	= '添加成功！';
						$date['status']	= '1';
						$date['callback']	= U('index');
						$this ->ajaxReturn($date);
					}else{
						$date['msg']	= '添加失败！';
						$date['status']	= '0';
						$this ->ajaxReturn($date);
					}
				}else{
					$date['msg']	= $Data->getError();
					$date['status']	= '0';
					$this ->ajaxReturn($date);
				}
			}
		}
		$this				-> assign('category',$category);
		$this				-> display('add');
	}
	public function edit(){
		$id					= I('id');
		$category			= M('Category') -> where($map) -> select();
		$category			= toLevel($category,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',0);
		$viewInfo			= M('Manage') -> where(array('id' => $id)) -> find();
		if(IS_AJAX){
			$Data = D("Manage");
			if(I("typeids") == ""){
				$date['msg']	= "负责栏目不能为空";
				$date['status']	= '0';
				$this ->ajaxReturn($date);
			}else{
				$admin_eidt = array(
					array('password','6,30','密码长度不正确',2,'length'),
					array('author','require','笔名不能为空！'),
					array('phone','/^(0?1[358]\d{9})$|^((0(10|2[1-3]|[3-9]\d{2}))?[1-9]\d{6,7})$/','电话格式错误！',2,),
					array('email','email','Email格式错误！',2)
				);
				if($Data -> validate($admin_eidt) -> create()){
					$password = I("password");
					if($password == ""){
						unset($Data -> password);
					}else{
						$Data -> password = md5($password);
					}
					$Data -> typeids = implode(",", I("typeids"));
					$Data -> last_time = date("Y-m-d H:i:s");
					if($Data -> save() !== false){
						$date['msg']	= '修改成功！';
						$date['status']	= '1';
						$date['callback']	= U('index');
						$this ->ajaxReturn($date);
					}else{
						$date['msg']	= '添加失败！';
						$date['status']	= '0';
						$this ->ajaxReturn($date);
					}
				}else{
					$date['msg']	= $Data->getError();
					$date['status']	= '0';
					$this ->ajaxReturn($date);
				}
			}
		}
		$this				-> assign('category',$category);
		$this				-> assign('viewInfo',$viewInfo);
		$this				-> display('edit');
	}
	public function password(){
		if(IS_AJAX){
			$admin_password = array(
				array('author','require','笔名不能为空！'),
				array('oldpassword','require','密码不能为空！'),
				array('oldpassword','checkPassword','密码错误',1,'callback'),
				array('password','6,30','密码长度不正确',2,'length'),
				array('repassword','password','确认密码不一致',0,'confirm'),
			);
			$Data = D('Manage');
			if($Data -> validate($admin_password) -> create()){
				$password = I("password");
				if($password == ""){
					unset($Data -> password);
				}else{
					$Data -> password = md5($password);
				}
				if($Data -> save() !== false){
					$date['msg']	= '修改成功！';
					$date['status']	= '1';
					$date['callback']	= U('password');
					$this ->ajaxReturn($date);
				}else{
					$date['msg']	= '修改失败！';
					$date['status']	= '0';
					$this ->ajaxReturn($date);
				}
			}else{
				$date['msg']	= $Data -> getError();
				$date['status']	= '0';
				$this ->ajaxReturn($date);
			}
		}
		$this -> display('password');
	}
}